package com.m.controller;

import com.m.enums.WebReturnCode;
import com.m.pojo.dto.scenemanager.AddSceneInfoDto;
import com.m.pojo.dto.scenemanager.SceneDimensionDto;
import com.m.pojo.dto.scenemanager.SceneInfoDto;
import com.m.pojo.dto.scenemanager.SceneSearchDto;
import com.m.service.ISceneService;
import com.m.utils.ResponseBuilder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 功能：<br/>
 *
 * @author miracle
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@RestController
@RequestMapping("/api/v1/scene")
@Api(tags = "场景管理")
public class SceneController {
    private static final Logger LOG = LoggerFactory.getLogger(SceneController.class);

    @Autowired
    private ISceneService sceneService;

    @ApiOperation(value = "新增场景")
    @RequestMapping(value = "/addScene", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity addScene(AddSceneInfoDto sceneInfoDto, @RequestParam(value = "jarFileInfo", required = false) MultipartFile file) {
        try {
            sceneService.addScene(sceneInfoDto, file);
            return ResponseBuilder.build(WebReturnCode.SUCCEED,"success", null);
        } catch (Exception e) {
            LOG.error("新增场景信息失败:" + e.getMessage(), e);
            return ResponseBuilder.build(WebReturnCode.FAILED,"新增场景信息失败:" + e.getMessage(), null);
        }
    }

    @ApiOperation(value = "删除场景")
    @RequestMapping(value = "/delScene", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity delScene(@RequestParam(value = "sceneId", required = true) Long sceneId) {
        try {
            sceneService.delScene(sceneId);
            return ResponseBuilder.build(WebReturnCode.SUCCEED,"success", null);
        } catch (Exception e) {
            LOG.error("删除场景失败:" + e.getMessage(), e);
            return ResponseBuilder.build(WebReturnCode.FAILED,"删除场景失败:" + e.getMessage(), null);
        }
    }

    @ApiOperation(value = "查询场景")
    @RequestMapping(value = "/getSceneInfo", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<List<SceneInfoDto>> getSceneInfo(@RequestBody SceneSearchDto sceneSearchDto) {
        try {
            // 查询场景
            return ResponseBuilder.build(WebReturnCode.SUCCEED,"success", sceneService.getSceneInfo(sceneSearchDto));
        } catch (Exception e) {
            LOG.error("查询场景失败:" + e.getMessage(), e);
            return ResponseBuilder.build(WebReturnCode.FAILED,"查询场景失败:" + e.getMessage(), null);
        }
    }

    @ApiOperation(value = "查询场景维度")
    @RequestMapping(value = "/getSceneDimInfo", method = RequestMethod.GET)
    @ResponseBody
    public ResponseEntity<List<SceneDimensionDto>> getSceneDimInfo(@RequestParam(value = "sceneId", required = true) Long sceneId) {
        try {
            // 查询场景
            return ResponseBuilder.build(WebReturnCode.SUCCEED,"success", sceneService.getSceneDimInfo(sceneId));
        } catch (Exception e) {
            LOG.error("查询场景维度失败:" + e.getMessage(), e);
            return ResponseBuilder.build(WebReturnCode.FAILED,"查询场景维度失败:" + e.getMessage(), null);
        }
    }
}
